﻿/**
 * Peter
 * Created by: Peter Development Team
 *    http://peter.codeplex.com/
 * 
 * GNU General Public License version 2 (GPLv2)
 *    http://peter.codeplex.com/license
 *    
 *  This code is provided on an AS IS basis, with no WARRANTIES,
 *  CONDITIONS or GUARANTEES of any kind.
 *  
 **/

using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using Peter.Common.Interfaces;
using Peter.Common.Utilities;
using Peter.Logic.Dialogs;
using Peter.View;

namespace Peter.Logic
{
   /// <summary>
   /// Model for the project manager.
   /// </summary>
   public sealed class ModelProjectManager
   {
      /// <summary>
      /// Initializes a new project manager.
      /// </summary>
      public ModelProjectManager ()
      {
         this.Workspace = new ModelWorkspace (this);
      }

      /// <summary>
      /// Gets the list of project types.
      /// </summary>
      [ImportMany (typeof (IPeterProjectType))]
      public IEnumerable<IPeterProjectType> ProjectTypes { get; set; }

      /// <summary>
      /// Gets the instance of peter.
      /// </summary>
      [Import (typeof (IPeter))]
      public IPeter Peter { get; set; }

      /// <summary>
      /// Gets the workspace.
      /// </summary>
      public ModelWorkspace Workspace { get; private set; }

      /// <summary>
      /// Occurs when the new project command is executed.
      /// </summary>
      public void NewProject ()
      {
         if (!ViewRegistry.Contains (typeof (ModelNewProject)))
            ViewRegistry.Register (typeof (ModelNewProject), typeof (ViewNewProject));

         this.Peter.ShowDialog (new ModelNewProject (this));
      }

      /// <summary>
      /// Occurs when the new project command is executed.
      /// </summary>
      public void OpenProject ()
      {
         if (!ViewRegistry.Contains (typeof (ModelOpenProject)))
            ViewRegistry.Register (typeof (ModelOpenProject), typeof (ViewOpenProject));

         this.Peter.ShowDialog (new ModelOpenProject (this));
      }

      /// <summary>
      /// Gets the peter project for the given project type.
      /// </summary>
      /// <param name="projectType">The full type of project.</param>
      /// <returns>The project type object.</returns>
      public IPeterProjectType GetProjectForType (string projectType)
      {
         return this.ProjectTypes.FirstOrDefault (peterProjectType => peterProjectType.OwnsProject (projectType));
      }
   }
}
